package com.tz.mybatis.generator.plugins;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.FullyQualifiedTable;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.TopLevelClass;
/**
 * 自定义属性常量类生成插件
 * @author yishui
 * @date 2018年7月13日
 * @version 0.0.1
 */
public class TableConstantPlugin extends PluginAdapter {
	@Override
	public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
		FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
		String beanClassName = introspectedTable.getBaseRecordType();
		String beanName = beanClassName.substring(beanClassName.lastIndexOf(".") + 1);
		List<GeneratedJavaFile> files = new ArrayList<>();
		FullyQualifiedJavaType tableConstant = new FullyQualifiedJavaType(
				this.properties.getProperty("targetPackage") + ".T" + beanName);
		TopLevelClass topLevelClass = new TopLevelClass(tableConstant);
		topLevelClass.setVisibility(JavaVisibility.PUBLIC);
		topLevelClass.setAbstract(false);
		topLevelClass.setFinal(true);
		StringBuilder sb = new StringBuilder();

		topLevelClass.addJavaDocLine("/**");
		sb.append(" * @ClassName: ");
		sb.append(topLevelClass.getType().getShortName());
		topLevelClass.addJavaDocLine(sb.toString());

		sb = new StringBuilder();
		sb.append("* @Description: ");
		sb.append(table.toString() + "表结构对应的常量类，定义字段名常量");
		topLevelClass.addJavaDocLine(sb.toString());

		sb = new StringBuilder();
		sb.append("* @author: yishui zhiyubujian@163.com");
		topLevelClass.addJavaDocLine(sb.toString());

		topLevelClass.addJavaDocLine(" */");

		List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
		for (IntrospectedColumn introspectedColumn : introspectedColumns) {
			Field field = new Field();
			field.setVisibility(JavaVisibility.PUBLIC);
			field.setStatic(true);
			field.setFinal(true);
			field.setType(new FullyQualifiedJavaType("java.lang.String"));
			field.setName(introspectedColumn.getActualColumnName().toUpperCase());
			field.setInitializationString("\"" + introspectedColumn.getActualColumnName() + "\"");
			addFieldComment(field, "@Fields " + table.toString() + "."
					+ introspectedColumn.getActualColumnName().toUpperCase() + ": " + introspectedColumn.getRemarks());
			topLevelClass.addField(field);
		}

		GeneratedJavaFile file = new GeneratedJavaFile(topLevelClass,
				this.context.getJavaModelGeneratorConfiguration().getTargetProject(), new DefaultJavaFormatter());
		files.add(file);
		return files;
	}

	public static final String lineSeparator;

	static {
		String ls = System.getProperty("line.separator");
		if (ls == null) {
			ls = "\n";
		}
		lineSeparator = ls;
	}

	public void addFieldComment(Field field, String doc) {
		StringBuilder sb = new StringBuilder();
		if ((doc != null) && (doc.length() != 0)) {
			field.addJavaDocLine("/**");
			sb.append(" * ");
			doc = doc.replaceAll(lineSeparator, "<br>" + lineSeparator + "\t * ");
			sb.append(doc);
			field.addJavaDocLine(sb.toString());
			field.addJavaDocLine(" */");
		}
	}
@Override
	public boolean validate(List<String> warnings) {
		return true;
	}
}